package com.mdd.car.service.impl;

import com.mdd.car.params.CarBrandParam;
import com.mdd.car.service.ICarBrandService;
import com.mdd.car.vo.CarBrandVo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.car.entity.CarBrand;
import com.mdd.car.mapper.CarBrandMapper;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author 幻速
 * @Date 2024/3/11 14:22
 **/
@Service
public class CarBrandServiceImpl extends ServiceImpl<CarBrandMapper, CarBrand> implements ICarBrandService {
    @Override
    public List<CarBrandVo> all() {
        LambdaQueryWrapper<CarBrand> carBrandLQW=new LambdaQueryWrapper<>();
        List<CarBrandVo> carBrandVos=new ArrayList<>();
        this.list(carBrandLQW).forEach(carBrand -> {
            CarBrandVo carBrandVo=new CarBrandVo();
            BeanUtil.copyProperties(carBrand,carBrandVo);
            carBrandVos.add(carBrandVo);
        });
        return carBrandVos;
    }

    @Override
    public PageResult<CarBrandVo> list(PageValidate pageValidate,String brandName) {
        int page = null != pageValidate ? pageValidate.getPageNo() : 1;
        int limit = null != pageValidate ? pageValidate.getPageSize() : 10;
        LambdaQueryWrapper<CarBrand> carBrandLQW=new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(brandName)) {
            carBrandLQW.like(CarBrand::getBrandName, brandName);
        }
        IPage<CarBrand> iPage = this.baseMapper.selectPage(new Page<>(page, limit), carBrandLQW);
        List<CarBrandVo> list = new LinkedList<>();
        for (CarBrand item : iPage.getRecords()) {
            CarBrandVo vo = new CarBrandVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public void add(CarBrandParam carBrandParam) {
        CarBrand carBrand=new CarBrand();
        BeanUtils.copyProperties(carBrandParam,carBrand);
        save(carBrand);
    }

    @Override
    public void edit(CarBrandParam carBrandParam) {
        CarBrand carBrand=new CarBrand();
        BeanUtils.copyProperties(carBrandParam,carBrand);
        updateById(carBrand);
    }

    @Override
    public void del(List<Long> ids) {
        this.baseMapper.deleteBatchIds(ids);
    }
}
